package com.primb.netlibs.exception;

import android.net.ParseException;
import android.os.Build;
import android.util.Log;

import com.alibaba.fastjson.JSONException;
import com.primb.netlibs.BuildConfig;
import com.tencent.bugly.crashreport.CrashReport;

import java.net.ConnectException;
import java.net.NoRouteToHostException;
import java.net.SocketException;
import java.net.SocketTimeoutException;

import retrofit2.adapter.rxjava.HttpException;

/**
 * Created by Chen on 2017/6/19.
 * 功能描述：
 */

public class ExceptionEngine {
    //对应HTTP的状态码
    private static final int UNAUTHORIZED = 401;
    private static final int FORBIDDEN = 403;
    private static final int NOT_FOUND = 404;
    private static final int REQUEST_TIMEOUT = 408;
    private static final int INTERNAL_SERVER_ERROR = 500;
    private static final int BAD_GATEWAY = 502;
    private static final int SERVICE_UNAVAILABLE = 503;
    private static final int GATEWAY_TIMEOUT = 504;

    public static final int LOGIN_TIMEOUT = 1000001;

    public static ApiException handleException(Throwable e) {
        ApiException ex;
        if (e instanceof HttpException) {             //HTTP错误
            HttpException httpException = (HttpException) e;
            ex = new ApiException(e, ERROR.HTTP_ERROR);
            switch (httpException.code()) {
                case UNAUTHORIZED:
                    ex.message = "服务器拒绝访问，请稍后重试";
                    break;
                case REQUEST_TIMEOUT:
                case GATEWAY_TIMEOUT:
                    ex.message = "网络连接超时，请检查网络后重试";
                    break;
                case INTERNAL_SERVER_ERROR:
                    ex.message = "服务器出现错误，请稍后重试";
                    break;
                case FORBIDDEN:
                case NOT_FOUND:
                case BAD_GATEWAY:
                case SERVICE_UNAVAILABLE:
                default:
                    ex.message = "网络连接错误，请检查网络后重试";  //均视为网络错误
                    break;
            }
            return ex;
        } else if (e instanceof NoRouteToHostException) {
            ex = new ApiException(e, ERROR.HTTP_ERROR);
            ex.message = "网络连接错误，请检查网络后重试";
            return ex;
        } else if (e instanceof SocketTimeoutException) {    //连接超时错误
            ex = new ApiException(e, ERROR.HTTP_ERROR);
            ex.message = "网络连接超时，请检查网络后重试";
            return ex;
        } else if (e instanceof ResultException) {
            ResultException resultException = (ResultException) e;
            ex = new ApiException(resultException, resultException.code);
            if (resultException.code == LOGIN_TIMEOUT) {
                ex.message = resultException.message;
                ex.code = LOGIN_TIMEOUT;
            } else {
                ex.message = resultException.message;
            }
            return ex;
        } else if (e instanceof JSONException
                || e instanceof ParseException) {
            ex = new ApiException(e, ERROR.PARSE_ERROR);
            ex.message = "服务器数据格式异常";            //均视为解析错误
            return ex;
        } else if (e instanceof ConnectException || e instanceof SocketException) {
            ex = new ApiException(e, ERROR.NETWORD_ERROR);
            ex.message = "网络连接错误，请检查网络后重试";  //均视为网络错误
            return ex;
        } else if (e instanceof ApiException) {
            return ((ApiException) e);
        } else {
            CrashReport.postCatchedException(e);//上传错误信息
            if (BuildConfig.DEBUG) {
                e.printStackTrace();
            }
            Log.e("error", e.getMessage());
            ex = new ApiException(e, ERROR.UNKNOWN);
            ex.message = "未知错误";          //未知错误
            return ex;
        }
    }
}
